package com.kikoulol.jarmory.extractor.xml;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.kikoulol.jarmory.extractor.BattleGroup;
import com.kikoulol.jarmory.extractor.Realm;
import com.kikoulol.jarmory.extractor.enums.Regions;

public class BattleGroups extends AbstractArmoryXmlFile
{
  private List<BattleGroup> battleGroups = new ArrayList<BattleGroup>();

  /**
   * 
   * @param region
   */
  public BattleGroups(Regions region)
  {
    super(region, "battlegroups.xml");
  }

  /**
   * 
   */
  @Override
  protected void initProperties()
  {
    Element battleGroupsElement = (Element) this.getXml().getChildNodes().item(1);
    NodeList battleGroupsList = battleGroupsElement.getElementsByTagName("battlegroup");

    // For each BattleGroup
    for (int i = 0; i < battleGroupsList.getLength(); i++)
    {
      // Realms for each battleGroup
      List<Realm> realms = new ArrayList<Realm>();

      Element battleGroupElement = (Element) (battleGroupsList.item(i));
      BattleGroup battleGroup = new BattleGroup();
      battleGroup.setDisplay(battleGroupElement.getAttribute("display"));
      battleGroup.setLadderUrl(battleGroupElement.getAttribute("ladderUrl"));
      battleGroup.setName(battleGroupElement.getAttribute("name"));

      NodeList realmsList = battleGroupElement.getElementsByTagName("realm");
      for (int j = 0; j < realmsList.getLength(); j++)
      {
        Realm realm = new Realm();
        realm.setName(this.getNamedItemValue(realmsList.item(j), "name"));
        realm.setNameEN(this.getNamedItemValue(realmsList.item(j), "nameEN"));
        realm.setNameUrl(this.getNamedItemValue(realmsList.item(j), "nameUrl"));
        realms.add(realm);
      }
      battleGroup.setRealms(realms);
      this.battleGroups.add(battleGroup);
    }
  }

  public List<BattleGroup> getBattleGroups()
  {
    super.setupXml();
    return battleGroups;
  }
}
